let cp = require('cp');
require('./page.css');
if (cp.getCookie('token') !== '' && cp.getCookie('token') !== 'undefined') {
  window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
}
let vm_login = avalon.define({
  $id: 'mc_login',
  isCode: false,
  username: '',
  password: '',
  validate: {
    onValidateAll: function(reasons) {
      // 获取用户列表
      if (reasons.length) {
        cp.layerMsg({
          msg: reasons[0].getMessage(),
        });
      } else {
        let md5 = require('js-md5');
        let Base64 = require('js-base64').Base64;
        cp.ajaxUrl({
          controller: 'account',
          checkUser: false,
          cmd: 'com.rel.porg.pb.acc_sign_in_req',
          data: {
            account: vm_login.username,
            passwd: Base64.encode(md5(vm_login.password).toUpperCase()),
          },
          success: function(res) {
            // body...
            cp.setCookie('token', res.token, 12);
            const libs = require('libs');
            libs.sendParamBetweenPages('common', 'orgid', res.info.orgid);
            if (res.token) {
              window.location.href = cp.PAGE_ROOT + 'index/index/page.html';
            }
          },
          fail: (res) => {
            cp.layerMsg({
              msg: res.desc,
            });
          },
        });
      }
    },
  },
  bindShowCode: function() {
    vm_login.isCode = !vm_login.isCode;
  },
});
